package {{packageName}}.core.{{entity.module}}.filter;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import java.util.*;
import java.io.Serializable;
{{#if entity.hasBigDecimal}}
import java.math.BigDecimal;
{{/if}}
import com.fasterxml.jackson.annotation.*;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.*;
import lombok.experimental.Accessors;
import lombok.extern.slf4j.Slf4j;
import com.alibaba.fastjson.annotation.JSONField;

import org.apache.commons.lang3.StringUtils;
import org.springframework.data.annotation.Transient;
import org.springframework.util.ObjectUtils;
{{#entity.enableES}}
import org.elasticsearch.index.query.QueryBuilders;
{{/entity.enableES}}
import cn.ibizlab.util.filter.QueryWrapperContext;
import cn.ibizlab.util.filter.QueryFilter;
import {{packageName}}.util.enums.Entities;
import {{packageName}}.core.{{entity.module}}.domain.{{entity.domainClassName}};

/**
 * 关系型数据实体[{{entity.codeName}}] 查询条件对象
 *
 * @author generator
 */
@Slf4j
@Getter
@Setter
@NoArgsConstructor
@Accessors(chain = true)
@ApiModel("{{entity.logicName}}查询条件对象")
public class {{entity.codeName}}SearchContext extends QueryWrapperContext<{{entity.domainClassName}}> {

    {{#each entity.fields as |field|}}
            {{#each field.allPSDEFSearchModes as |formitem|}}
            {{#unless formitem.valueFunc}}
    /**
     * {{field.logicName}}{{formitem.valueOP}}
     */
                    {{#if (or (eq formitem.valueOP "IN") (eq formitem.valueOP "ISNULL") (eq formitem.valueOP "ISNOTNULL") (formitem.dstPSDEField))}}
    @JsonProperty("{{lowerCase formitem.name}}")
    @JSONField(name = "{{lowerCase formitem.name}}")
                    {{else}}
    @JsonProperty("{{lowerCase formitem.name}}")
                        {{#timeType}}
    @JsonFormat(pattern = "{{format}}", locale = "zh", timezone = "GMT+8")
                        {{/timeType}}
    @JSONField(name = "{{lowerCase formitem.name}}"{{#timeType}} , format = "{{format}}"{{/timeType}})
                        {{#eq type.java "Long"}}
    @JsonSerialize(using = ToStringSerializer.class)            
                        {{/eq}}
                    {{/if}}
    @ApiModelProperty("{{field.logicName}}{{formitem.valueOP}}")
    private {{#if (or (eq formitem.valueOP "IN") (eq formitem.valueOP "ISNULL") (eq formitem.valueOP "ISNOTNULL") (formitem.dstPSDEField))}}String{{else}}{{field.javaType}}{{/if}} {{camelCase field.codeName}}{{formitem.valueOP}}{{formitem.dstPSDEField.codeName}}{{formitem.dstPSDEFSearchMode.valueOP}};

            {{/unless}}
            {{/each}}
    {{/each}}
    {{#entity.hasReferences}}
    @Override
    public void setContextParentKey(Serializable contextParentKey) {
        super.setContextParentKey(contextParentKey);
        {{#each entity.references as |reference|}}
            {{#if reference.fkField}}
        if(Entities.{{reference.relEntity.definitionName}}.equals(this.getContextParentEntity())&&contextParentKey!=null)
            this.getFilter().eq("{{lowerCase reference.fkField.name}}",contextParentKey);
            {{/if}}
        {{/each}}
    }

    {{/entity.hasReferences}}
    @Override
    public void setQuery(String query) {
        this.query=query;
        if(!ObjectUtils.isEmpty(query))
            this.getFilter().and(QueryFilter.createQuery().or({{#each entity.quickSearchFields as |field|}}{{#unless @first}},{{/unless}}QueryFilter.createQuery().like("{{lowerCase field.name}}",query){{/each}}));
    }

    @JsonIgnore
    @JSONField(serialize = false)
    @Transient
    @Override
    public QueryWrapper<{{entity.domainClassName}}> getSelectCond() {
        QueryWrapper<{{entity.domainClassName}}> queryWrapper = super.getSelectCond();
    {{#each entity.fields as |field|}}
        {{#each field.allPSDEFSearchModes as |formitem|}}
            {{#unless formitem.valueFunc}}
                        {{#eq formitem.valueOP "EXISTS"}}
        if (!ObjectUtils.isEmpty(this.{{camelCase field.codeName}}{{formitem.valueOP}}{{formitem.dstPSDEField.codeName}}{{formitem.dstPSDEFSearchMode.valueOP}})) {
            queryWrapper.exists("SELECT 1 FROM {{upperCase formitem.pSDER.minorPSDataEntity.tableName}} WHERE {{upperCase formitem.pSDER.minorPSDataEntity.tableName}}.{{upperCase formitem.pSDER.pSPickupDEField.name}} = T1.{{upperCase formitem.pSDER.pSPickupDEField.relatedPSDEField.name}} "
                + " AND {{upperCase formitem.pSDER.minorPSDataEntity.tableName}}.{{upperCase formitem.dstPSDEField.name}} "
                    {{#eq formitem.dstPSDEFSearchMode.valueOP "LIKE"}}
                + " LIKE "
                    {{/eq}}
                    {{#eq formitem.dstPSDEFSearchMode.valueOP "LEFTLIKE"}}
                + " LIKE "
                    {{/eq}}
                    {{#eq formitem.dstPSDEFSearchMode.valueOP "RIGHTLIKE"}}
                + " LIKE "
                    {{/eq}}
                    {{#eq formitem.dstPSDEFSearchMode.valueOP "EQ"}}
                + " = "
                    {{/eq}}
                    {{#eq formitem.dstPSDEFSearchMode.valueOP "NOTEQ"}}
                + " <> "
                    {{/eq}}
                    {{#eq formitem.dstPSDEFSearchMode.valueOP "GT"}}
                + " > "
                    {{/eq}}
                    {{#eq formitem.dstPSDEFSearchMode.valueOP "GTANDEQ"}}
                + " >= "
                    {{/eq}}
                    {{#eq formitem.dstPSDEFSearchMode.valueOP "LT"}}
                + " < "
                    {{/eq}}
                    {{#eq formitem.dstPSDEFSearchMode.valueOP "LTANDEQ"}}
                + " <= "
                    {{/eq}}
                    {{#eq formitem.dstPSDEFSearchMode.valueOP "ISNOTNULL"}}
                + " IS NOT NULL" 
                    {{/eq}}
                    {{#eq formitem.dstPSDEFSearchMode.valueOP "ISNULL"}}
                + " IS NULL" 
                    {{/eq}}
                    {{#eq formitem.dstPSDEFSearchMode.valueOP "IN"}}
                + " IN "
                    {{/eq}}
                    {{#eq formitem.dstPSDEFSearchMode.valueOP "NOTIN"}}
                + " NOT IN "
                    {{/eq}}
                    {{#if (or (eq formitem.dstPSDEFSearchMode.valueOP "ISNOTNULL") (eq formitem.dstPSDEFSearchMode.valueOP "ISNULL")) }}
                    {{else}}
                    {{#if (or (eq formitem.dstPSDEFSearchMode.valueOP "IN") (eq formitem.dstPSDEFSearchMode.valueOP "NOTIN")) }}
                        {{#if (or (eq formitem.dstPSDEField.dataType "BIGINT") (eq formitem.dstPSDEField.dataType "DECIMAL") (eq formitem.dstPSDEField.dataType "FLOAT") (eq formitem.dstPSDEField.dataType "INT") (eq formitem.dstPSDEField.dataType "MONEY"))}}
                + "(" + StringUtils.join(this.{{camelCase field.codeName}}{{formitem.valueOP}}{{formitem.dstPSDEField.codeName}}{{formitem.dstPSDEFSearchMode.valueOP}}.split(";|,|；|，"),",") + ")"
                        {{else}}
                + "('" + StringUtils.join(this.{{camelCase field.codeName}}{{formitem.valueOP}}{{formitem.dstPSDEField.codeName}}{{formitem.dstPSDEFSearchMode.valueOP}}.split(";|,|；|，"),"','") + "')"
                        {{/if}}
                    {{else}}
                        {{#if (or (eq formitem.dstPSDEFSearchMode.valueOP "LIKE") (eq formitem.dstPSDEFSearchMode.valueOP "LEFTLIKE") (eq formitem.dstPSDEFSearchMode.valueOP "RIGHTLIKE"))}}
                                {{#eq formitem.dstPSDEFSearchMode.valueOP "LIKE"}}
                + "'%" + this.{{camelCase field.codeName}}{{formitem.valueOP}}{{formitem.dstPSDEField.codeName}}{{formitem.dstPSDEFSearchMode.valueOP}} + "%'"
                                {{/eq}}
                                {{#eq formitem.dstPSDEFSearchMode.valueOP "LEFTLIKE"}}
                + "'" + this.{{camelCase field.codeName}}{{formitem.valueOP}}{{formitem.dstPSDEField.codeName}}{{formitem.dstPSDEFSearchMode.valueOP}} + "%'"
                                {{/eq}}
                                {{#eq formitem.dstPSDEFSearchMode.valueOP "RIGHTLIKE"}}
                + "'%" + this.{{camelCase field.codeName}}{{formitem.valueOP}}{{formitem.dstPSDEField.codeName}}{{formitem.dstPSDEFSearchMode.valueOP}} + "'"
                                {{/eq}}
                        {{else}}
                        {{#if (or (eq formitem.dstPSDEField.dataType "BIGINT") (eq formitem.dstPSDEField.dataType "DECIMAL") (eq formitem.dstPSDEField.dataType "FLOAT") (eq formitem.dstPSDEField.dataType "INT") (eq formitem.dstPSDEField.dataType "MONEY"))}}
                + this.{{camelCase field.codeName}}{{formitem.valueOP}}{{formitem.dstPSDEField.codeName}}{{formitem.dstPSDEFSearchMode.valueOP}}
                        {{else}}
                + "'" + this.{{camelCase field.codeName}}{{formitem.valueOP}}{{formitem.dstPSDEField.codeName}}{{formitem.dstPSDEFSearchMode.valueOP}} + "'"
                        {{/if}}
                        {{/if}}
                    {{/if}}
                    {{/if}}
            );
        }
                    {{/eq}}
                    {{#eq formitem.valueOP "NOTEXISTS"}}
        if (!ObjectUtils.isEmpty(this.{{camelCase field.codeName}}{{formitem.valueOP}}{{formitem.dstPSDEField.codeName}}{{formitem.dstPSDEFSearchMode.valueOP}})) {
            queryWrapper.notExists("SELECT 1 FROM {{upperCase formitem.pSDER.minorPSDataEntity.tableName}} WHERE {{upperCase formitem.pSDER.minorPSDataEntity.tableName}}.{{upperCase formitem.pSDER.pSPickupDEField.name}} = T1.{{upperCase formitem.pSDER.pSPickupDEField.relatedPSDEField.name}} "
                + " AND {{upperCase formitem.pSDER.minorPSDataEntity.tableName}}.{{upperCase formitem.dstPSDEField.name}} "
                    {{#eq formitem.dstPSDEFSearchMode.valueOP "LIKE"}}
                + " LIKE "
                    {{/eq}}
                    {{#eq formitem.dstPSDEFSearchMode.valueOP "LEFTLIKE"}}
                + " LIKE "
                    {{/eq}}
                    {{#eq formitem.dstPSDEFSearchMode.valueOP "RIGHTLIKE"}}
                + " LIKE "
                    {{/eq}}
                    {{#eq formitem.dstPSDEFSearchMode.valueOP "EQ"}}
                + " = "
                    {{/eq}}
                    {{#eq formitem.dstPSDEFSearchMode.valueOP "NOTEQ"}}
                + " <> "
                    {{/eq}}
                    {{#eq formitem.dstPSDEFSearchMode.valueOP "GT"}}
                + " > "
                    {{/eq}}
                    {{#eq formitem.dstPSDEFSearchMode.valueOP "GTANDEQ"}}
                + " >= "
                    {{/eq}}
                    {{#eq formitem.dstPSDEFSearchMode.valueOP "LT"}}
                + " < "
                    {{/eq}}
                    {{#eq formitem.dstPSDEFSearchMode.valueOP "LTANDEQ"}}
                + " <= "
                    {{/eq}}
                    {{#eq formitem.dstPSDEFSearchMode.valueOP "ISNOTNULL"}}
                + " IS NOT NULL" 
                    {{/eq}}
                    {{#eq formitem.dstPSDEFSearchMode.valueOP "ISNULL"}}
                + " IS NULL" 
                    {{/eq}}
                    {{#eq formitem.dstPSDEFSearchMode.valueOP "IN"}}
                + " IN "
                    {{/eq}}
                    {{#eq formitem.dstPSDEFSearchMode.valueOP "NOTIN"}}
                + " NOT IN "
                    {{/eq}}
                    {{#if (or (eq formitem.dstPSDEFSearchMode.valueOP "ISNOTNULL") (eq formitem.dstPSDEFSearchMode.valueOP "ISNULL")) }}
                    {{else}}
                    {{#if (or (eq formitem.dstPSDEFSearchMode.valueOP "IN") (eq formitem.dstPSDEFSearchMode.valueOP "NOTIN")) }}
                        {{#if (or (eq formitem.dstPSDEField.dataType "BIGINT") (eq formitem.dstPSDEField.dataType "DECIMAL") (eq formitem.dstPSDEField.dataType "FLOAT") (eq formitem.dstPSDEField.dataType "INT") (eq formitem.dstPSDEField.dataType "MONEY"))}}
                + "(" + StringUtils.join(this.{{camelCase field.codeName}}{{formitem.valueOP}}{{formitem.dstPSDEField.codeName}}{{formitem.dstPSDEFSearchMode.valueOP}}.split(";|,|；|，"),",") + ")"
                        {{else}}
                + "('" + StringUtils.join(this.{{camelCase field.codeName}}{{formitem.valueOP}}{{formitem.dstPSDEField.codeName}}{{formitem.dstPSDEFSearchMode.valueOP}}.split(";|,|；|，"),"','") + "')"
                        {{/if}}
                    {{else}}
                        {{#if (or (eq formitem.dstPSDEFSearchMode.valueOP "LIKE") (eq formitem.dstPSDEFSearchMode.valueOP "LEFTLIKE") (eq formitem.dstPSDEFSearchMode.valueOP "RIGHTLIKE"))}}
                                {{#eq formitem.dstPSDEFSearchMode.valueOP "LIKE"}}
                + "'%" + this.{{camelCase field.codeName}}{{formitem.valueOP}}{{formitem.dstPSDEField.codeName}}{{formitem.dstPSDEFSearchMode.valueOP}} + "%'"
                                {{/eq}}
                                {{#eq formitem.dstPSDEFSearchMode.valueOP "LEFTLIKE"}}
                + "'" + this.{{camelCase field.codeName}}{{formitem.valueOP}}{{formitem.dstPSDEField.codeName}}{{formitem.dstPSDEFSearchMode.valueOP}} + "%'"
                                {{/eq}}
                                {{#eq formitem.dstPSDEFSearchMode.valueOP "RIGHTLIKE"}}
                + "'%" + this.{{camelCase field.codeName}}{{formitem.valueOP}}{{formitem.dstPSDEField.codeName}}{{formitem.dstPSDEFSearchMode.valueOP}} + "'"
                                {{/eq}}
                        {{else}}
                        {{#if (or (eq formitem.dstPSDEField.dataType "BIGINT") (eq formitem.dstPSDEField.dataType "DECIMAL") (eq formitem.dstPSDEField.dataType "FLOAT") (eq formitem.dstPSDEField.dataType "INT") (eq formitem.dstPSDEField.dataType "MONEY"))}}
                + this.{{camelCase field.codeName}}{{formitem.valueOP}}{{formitem.dstPSDEField.codeName}}{{formitem.dstPSDEFSearchMode.valueOP}}
                        {{else}}
                + "'" + this.{{camelCase field.codeName}}{{formitem.valueOP}}{{formitem.dstPSDEField.codeName}}{{formitem.dstPSDEFSearchMode.valueOP}} + "'"
                        {{/if}}
                        {{/if}}
                    {{/if}}
                    {{/if}}
            );
        }
                    {{/eq}}
            {{/unless}}
        {{/each}}
    {{/each}}
        return queryWrapper;
    }
}